﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml;

namespace FileExplorerApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadDrives();
        }

        // Represents a file/folder item
        public class FileItem
        {
            public string Name { get; set; }
            public string Type { get; set; }
            public string Size { get; set; }
            public string Path { get; set; }
        }

        // Load all drives into the TreeView
        private void LoadDrives()
        {
            TreeViewItem root = new TreeViewItem() { Header = "This PC" };
            foreach (var drive in DriveInfo.GetDrives())
            {
                if (drive.IsReady)
                {
                    TreeViewItem driveItem = new TreeViewItem() { Header = drive.Name, Tag = drive.RootDirectory.FullName };
                    driveItem.Items.Add(null); // Placeholder for folder loading
                    driveItem.Expanded += DriveItem_Expanded;
                    root.Items.Add(driveItem);
                }
            }
            treeView.Items.Add(root);
        }

        // Handle folder expansion in the TreeView
        private void DriveItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            string path = item.Tag.ToString();
            item.Items.Clear();

            try
            {
                foreach (var directory in Directory.GetDirectories(path))
                {
                    TreeViewItem folderItem = new TreeViewItem() { Header = new DirectoryInfo(directory).Name, Tag = directory };
                    folderItem.Items.Add(null); // Placeholder for folder loading
                    folderItem.Expanded += FolderItem_Expanded;
                    item.Items.Add(folderItem);
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        // Handle folder expansion in the TreeView
        private void FolderItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            string path = item.Tag.ToString();
            item.Items.Clear();

            try
            {
                foreach (var directory in Directory.GetDirectories(path))
                {
                    TreeViewItem folderItem = new TreeViewItem() { Header = new DirectoryInfo(directory).Name, Tag = directory };
                    folderItem.Items.Add(null); // Placeholder for folder loading
                    folderItem.Expanded += FolderItem_Expanded;
                    item.Items.Add(folderItem);
                }
                foreach (var file in Directory.GetFiles(path))
                {
                    item.Items.Add(new TreeViewItem() { Header = Path.GetFileName(file), Tag = file });
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        // Load files when selecting a folder in TreeView
        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem selectedItem = treeView.SelectedItem as TreeViewItem;
            if (selectedItem != null && Directory.Exists(selectedItem.Tag.ToString()))
            {
                listView.Items.Clear();
                LoadFiles(selectedItem.Tag.ToString());
            }

            _ = treeView.SelectedItem as TreeViewItem;
            if (selectedItem != null)
            {
                MessageBox.Show($"Selected item: {selectedItem.Header}");
            }
        }

        // Load files into the ListView
        private void LoadFiles(string path)
        {
            try
            {
                foreach (var directory in Directory.GetDirectories(path))
                {
                    listView.Items.Add(new FileItem()
                    {
                        Name = new DirectoryInfo(directory).Name,
                        Type = "Folder",
                        Size = "-",
                        Path = directory
                    });
                }

                foreach (var file in Directory.GetFiles(path))
                {
                    listView.Items.Add(new FileItem()
                    {
                        Name = Path.GetFileName(file),
                        Type = "File",
                        Size = new FileInfo(file).Length.ToString() + " bytes",
                        Path = file
                    });
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        // Handle double-click on a file in ListView
        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var selectedFile = listView.SelectedItem as FileItem;
            if (selectedFile != null && selectedFile.Path.EndsWith(".xml"))
            {
                DisplayXmlContent(selectedFile.Path);
            }
        }

        // Display XML content in a new window
        private void DisplayXmlContent(string filePath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);

            string content = $"XML Content of {filePath}:\n\n";
            foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
            {
                content += $"Element: {node.Name}, Attributes: {node.Attributes?.Count}, Text: {node.InnerText}\n";
            }

            MessageBox.Show(content, "XML Content", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
